package org.dromara.third.convert.trouble.validator;

import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.convert.trouble.dto.TroubleDTO;
import org.dromara.third.convert.util.ExistenceValidator;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import java.util.Objects;

public class TroubleValidator implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        return TroubleDTO.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        TroubleDTO troubleDTO = (TroubleDTO) target;

        // 基础字段验证
        validateRequiredFields(troubleDTO, errors);

        // 条件验证
        validateConditionalFields(troubleDTO, errors);

        // 存在性验证
        validateExistence(troubleDTO, errors);
    }

    /**
     * 验证必填字段
     */
    private void validateRequiredFields(TroubleDTO dto, Errors errors) {
        ValidateUtil.rejectIfNull(dto::getId, String.valueOf(ExceptionEnum.AuthException01.getCode()), "主键不能为空", errors);
        ValidateUtil.rejectIfNull(dto::getDangerName, String.valueOf(ExceptionEnum.AuthException01.getCode()), "隐患名称不能为空", errors);
        ValidateUtil.rejectIfNull(dto::getRiskMeasureId, String.valueOf(ExceptionEnum.AuthException01.getCode()), "管控措施ID不能为空", errors);
        ValidateUtil.rejectIfNull(dto::getRecordId, String.valueOf(ExceptionEnum.AuthException01.getCode()), "隐患排查记录ID不能为空", errors);
        ValidateUtil.rejectIfNull(dto::getDangerLevel, String.valueOf(ExceptionEnum.AuthException01.getCode()), "隐患等级不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getRegistTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "登记时间不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getRegistrant, String.valueOf(ExceptionEnum.AuthException01.getCode()), "登记人姓名不能为空", errors);
        ValidateUtil.rejectIfNull(dto::getDangerDesc, String.valueOf(ExceptionEnum.AuthException01.getCode()), "隐患来源不能为空", errors);
        ValidateUtil.rejectIfNull(dto::getDangerManageType, String.valueOf(ExceptionEnum.AuthException01.getCode()), "治理类型不能为空", errors);
        ValidateUtil.rejectIfNull(dto::getHazardDangerType, String.valueOf(ExceptionEnum.AuthException01.getCode()), "隐患类型不能为空", errors);
        ValidateUtil.rejectIfNull(dto::getHazardCategory, String.valueOf(ExceptionEnum.AuthException01.getCode()), "隐患类别不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getDangerDesc, String.valueOf(ExceptionEnum.AuthException01.getCode()), "隐患描述不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getLiablePerson, String.valueOf(ExceptionEnum.AuthException01.getCode()), "整改责任人不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getDangerManageDeadline, String.valueOf(ExceptionEnum.AuthException01.getCode()), "隐患治理期限不能为空", errors);
        ValidateUtil.rejectIfNull(dto::getState, String.valueOf(ExceptionEnum.AuthException01.getCode()), "隐患状态不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getAttachments1, String.valueOf(ExceptionEnum.AuthException01.getCode()), "整改前照片不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getAttachments2, String.valueOf(ExceptionEnum.AuthException01.getCode()), "整改后照片不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getDelFlag, String.valueOf(ExceptionEnum.AuthException01.getCode()), "删除标志不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getCreateDept, String.valueOf(ExceptionEnum.AuthException01.getCode()), "所属部门不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getCreateBy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "创建人不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getCreateTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "创建时间不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getUpdateBy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "更新人不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getUpdateTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "更新时间不能为空", errors);
    }

    /**
     * 验证条件字段
     */
    private void validateConditionalFields(TroubleDTO dto, Errors errors) {
        // 如果隐患状态为已验收，则必须填写验收人姓名和验收时间
        if (Objects.equals(9L, dto.getState())) {
            ValidateUtil.rejectIfEmpty(dto::getCheckAcceptPerson, String.valueOf(ExceptionEnum.AuthException01.getCode()), "验收人姓名不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getCheckAcceptTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "验收时间不能为空", errors);
        }
    }

    /**
     * 验证存在性
     */
    private void validateExistence(TroubleDTO dto, Errors errors) {
        // 验证所属部门存在
        ExistenceValidator.validateDeptExists(dto.getCreateDept(), "所属部门", errors);

        // 验证创建人存在
        ExistenceValidator.validateUserExists(dto.getCreateBy(), "创建人", errors);

        // 验证更新人存在
        ExistenceValidator.validateUserExists(dto.getUpdateBy(), "更新人", errors);
    }
}
